package com.qiaoyanqing.ximalaya.discovermodule.discover_activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.BroadcastTypeAdapter;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.Broadcast_HttpService;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.BroadcastLocalEntity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.BroadcastResultEntity;
import com.qiaoyanqing.ximalaya.url.Urlconfig;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class BroadcastTypeActivity extends AppCompatActivity implements Callback<BroadcastLocalEntity> {
    private TextView type_tv;
    private ImageView back_iv;
    private RecyclerView broadcast_type;
    private int page=1;
    private Broadcast_HttpService service;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broadcast_type);
        initView();
    }

    private void initView() {
        type_tv = (TextView) findViewById(R.id.type_tv);
        back_iv = (ImageView) findViewById(R.id.back_iv);
        broadcast_type = (RecyclerView) findViewById(R.id.broadcase_type);

        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        String type = getIntent().getStringExtra("type");
        type_tv.setText(type);
        service = new Retrofit.Builder()
                .baseUrl(Urlconfig.BROADCASR)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(Broadcast_HttpService.class);
        if (type.equals("本地台")){
            getLocal(type);
        }else if (type.equals("国家台")){
            getCountry(type);
        }else if (type.equals("省市台")){
            getProvience(type);
        }else if (type.equals("网络台")){
            getInternet(type);
        }

    }

    private void getInternet(String type) {

    }

    private void getProvience(String type) {
    }

    private void getCountry(String type) {
       service.getCountryData(page).enqueue(new Callback<BroadcastLocalEntity>() {
           @Override
           public void onResponse(Call<BroadcastLocalEntity> call, Response<BroadcastLocalEntity> response) {
               List<BroadcastResultEntity> countryList = response.body().getResult();
               BroadcastTypeAdapter adapter = new BroadcastTypeAdapter(BroadcastTypeActivity.this, countryList);
               broadcast_type.setAdapter(adapter);
               adapter.notifyDataSetChanged();
           }

           @Override
           public void onFailure(Call<BroadcastLocalEntity> call, Throwable t) {
               t.printStackTrace();
               Toast.makeText(BroadcastTypeActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
           }
       });

    }

    private void getLocal(String type) {

        service.getLocalData(page).enqueue(this);
    }


    @Override
    public void onResponse(Call<BroadcastLocalEntity> call, Response<BroadcastLocalEntity> response) {
        List<BroadcastResultEntity> list = response.body().getResult();
        Log.d("=====", "onResponse: "+list.size());
        broadcast_type.setAdapter(new BroadcastTypeAdapter(this,list));
    }

    @Override
    public void onFailure(Call<BroadcastLocalEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
